import tkinter as tk
from tkinter import colorchooser, ttk, messagebox
import math

class PaintingTool:
    def __init__(self, root):
        # 设置主窗口
        self.root = root
        self.root.title("简易绘画工具")
        self.root.geometry("800x600")
        self.root.resizable(True, True)
        
        # 绘画参数
        self.brush_color = "black"
        self.brush_size = 5
        self.eraser_size = 20
        self.is_drawing = False
        self.last_x = None
        self.last_y = None
        self.current_tool = "brush"  # 默认工具是画笔
        
        # 创建UI
        self.create_widgets()
        
    def create_widgets(self):
        # 创建顶部工具栏
        toolbar = ttk.Frame(self.root, padding="5")
        toolbar.pack(side=tk.TOP, fill=tk.X)
        
        # 颜色选择按钮
        ttk.Button(toolbar, text="选择颜色", command=self.choose_color).pack(side=tk.LEFT, padx=2)
        
        # 画笔大小选择
        ttk.Label(toolbar, text="画笔大小:").pack(side=tk.LEFT, padx=2)
        self.brush_size_var = tk.IntVar(value=self.brush_size)
        ttk.Combobox(toolbar, textvariable=self.brush_size_var, 
                    values=[1, 3, 5, 10, 15, 20, 30], width=5).pack(side=tk.LEFT, padx=2)
        ttk.Button(toolbar, text="应用", command=self.set_brush_size).pack(side=tk.LEFT, padx=2)
        
        # 工具选择
        ttk.Label(toolbar, text="工具:").pack(side=tk.LEFT, padx=2)
        self.tool_var = tk.StringVar(value=self.current_tool)
        ttk.Radiobutton(toolbar, text="画笔", variable=self.tool_var, 
                       value="brush", command=self.set_tool).pack(side=tk.LEFT, padx=1)
        ttk.Radiobutton(toolbar, text="橡皮擦", variable=self.tool_var, 
                       value="eraser", command=self.set_tool).pack(side=tk.LEFT, padx=1)
        
        # 清空画布按钮
        ttk.Button(toolbar, text="清空画布", command=self.clear_canvas).pack(side=tk.RIGHT, padx=2)
        
        # 创建画布
        self.canvas_frame = ttk.Frame(self.root, padding="5")
        self.canvas_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
        
        self.canvas = tk.Canvas(self.canvas_frame, bg="white")
        self.canvas.pack(fill=tk.BOTH, expand=True)
        
        # 绑定鼠标事件
        self.canvas.bind("<Button-1>", self.start_drawing)
        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.stop_drawing)
        
        # 添加右键菜单
        self.create_context_menu()
        
    def create_context_menu(self):
        """创建右键菜单"""
        self.context_menu = tk.Menu(self.root, tearoff=0)
        self.context_menu.add_command(label="选择颜色", command=self.choose_color)
        self.context_menu.add_command(label="清空画布", command=self.clear_canvas)
        self.context_menu.add_separator()
        self.context_menu.add_command(label="关于", command=self.show_about)
        
        # 绑定右键菜单
        self.canvas.bind("<Button-3>", self.show_context_menu)
        
    def show_context_menu(self, event):
        """显示右键菜单"""
        self.context_menu.post(event.x_root, event.y_root)
        
    def show_about(self):
        """显示关于对话框"""
        messagebox.showinfo("关于", "简易绘画工具\n一个用Python Tkinter制作的简单绘画程序")
        
    def choose_color(self):
        """选择画笔颜色"""
        color = colorchooser.askcolor(title="选择颜色")[1]
        if color:
            self.brush_color = color
            
    def set_brush_size(self):
        """设置画笔大小"""
        self.brush_size = self.brush_size_var.get()
        
    def set_tool(self):
        """设置当前工具"""
        self.current_tool = self.tool_var.get()
        
    def start_drawing(self, event):
        """开始绘画"""
        self.is_drawing = True
        self.last_x = event.x
        self.last_y = event.y
        
        # 如果是点击而不是拖动，画一个点
        current_size = self.brush_size if self.current_tool == "brush" else self.eraser_size
        current_color = self.brush_color if self.current_tool == "brush" else "white"
        
        self.canvas.create_oval(
            event.x - current_size/2, event.y - current_size/2,
            event.x + current_size/2, event.y + current_size/2,
            fill=current_color, outline=current_color
        )
        
    def draw(self, event):
        """绘画过程"""
        if self.is_drawing and self.last_x and self.last_y:
            current_size = self.brush_size if self.current_tool == "brush" else self.eraser_size
            current_color = self.brush_color if self.current_tool == "brush" else "white"
            
            # 绘制线条
            self.canvas.create_line(
                self.last_x, self.last_y, event.x, event.y,
                width=current_size, fill=current_color,
                capstyle=tk.ROUND, smooth=tk.TRUE
            )
            
            # 更新最后位置
            self.last_x = event.x
            self.last_y = event.y
            
    def stop_drawing(self, event):
        """停止绘画"""
        self.is_drawing = False
        self.last_x = None
        self.last_y = None
        
    def clear_canvas(self):
        """清空画布"""
        if messagebox.askyesno("确认", "确定要清空画布吗?"):
            self.canvas.delete("all")

if __name__ == "__main__":
    root = tk.Tk()
    app = PaintingTool(root)
    root.mainloop()
    